1 module tests.person; 2 3 version(D_Ddoc) 4 { 5 /// Really important class 6 class Example { } 7 } 8 /** 9 This is just an example of stuff 10 */ 11 unittest 12 { 13 import db_constraints; 14 @CheckConstraint!(a => a.firstName != a.lastName, "chk_First_Last") 15 class Person 16 { 17 private: 18 Nullable!int _id; 19 string _firstName; 20 string _lastName; 21 string _email; 22 public: 23 @PrimaryKeyColumn @NotNull 24 @property Nullable!int id() const nothrow pure @safe @nogc 25 { 26 return _id; 27 } 28 @UniqueConstraintColumn!("uc_Person") 29 @property string firstName() const nothrow pure @safe @nogc 30 { 31 return _firstName; 32 } 33 @property void firstName(string value) 34 { 35 setter(_firstName, value); 36 } 37 @UniqueConstraintColumn!("uc_PersonEmail") 38 @property string email() const nothrow pure @safe @nogc 39 { 40 return _email; 41 } 42 @property void email(string value) 43 { 44 setter(_email, value); 45 } 46 @UniqueConstraintColumn!("uc_Person") 47 @property string lastName() const nothrow pure @safe @nogc 48 { 49 return _lastName; 50 } 51 52 this(string firstName_, string lastName_, string email_) 53 { 54 static int i = 1; 55 this._id = i; 56 this._firstName = firstName_; 57 this._lastName = lastName_; 58 this._email = email_; 59 ++i; 60 initializeKeyedItem(); 61 } 62 Person dup() 63 { 64 return new Person(this._firstName, this._lastName, this._email); 65 } 66 mixin KeyedItem!(UniqueConstraintColumn!("uc_PersonEmail")); 67 } 68 69 { 70 alias People = BaseKeyedCollection!(Person); 71 auto people = new People([new Person("Valid", "Person", "vp@test.com"), new Person("Second", "Sup", "s@e.org")]); 72 assert(people.contains("s@e.org")); 73 //assert(!people.contains(null)); 74 people["s@e.org"].email = "hello@all"; 75 assert(people.contains("hello@all")); 76 people["hello@all"].email = null; 77 assert(!people.contains("s@e.org")); 78 assert(!people.contains("hello@all")); 79 auto i = Person.uc_PersonEmail(); 80 i.email = null; 81 assert(people.contains(i)); 82 } 83 { 84 import std.exception : assertThrown; 85 assertThrown!CheckConstraintException(new Person("Name", "Name", "Name@org")); 86 } 87 }